<?php 
// Set flag that this is a parent file.
define('_JEXEC', 1);
define('DS', DIRECTORY_SEPARATOR);

if (file_exists(dirname(__FILE__) . '/defines.php')) {
	include_once dirname(__FILE__) . '/defines.php';
}

if (!defined('_JDEFINES')) {
	define('JPATH_BASE', dirname(__FILE__));
	require_once JPATH_BASE.'/includes/defines.php';
}

require_once ('libraries/lib-soap/nusoap.php');
require_once ('./configuration.php');
require_once (JPATH_BASE . '/components/com_manager/helpers/manager.php');
	
$server = new soap_server;
$server->configureWSDL('ReceiveMOowsdl', 'urn:ReceiveMOwsdl');

$server->soap_defencoding = 'UTF-8';
$server->decode_utf8 = false;

$server->register('ReceiveMO', // method name
    array( // input parameters	
    	'moid' => 'xsd:string',
    	'moseq' => 'xsd:string',
    	'src' => 'xsd:string',
    	'dest' => 'xsd:string',
    	'cmdcode' => 'xsd:string',
    	'msgbody' => 'xsd:string',
    	'opid' => 'xsd:string',
    	'username' => 'xsd:string',
    	'password' => 'xsd:string',
    ),        
    array('return' => 'xsd:int'), // output parameters
    'urn:ReceiveMOowsdl', // namespace
    'urn:ReceiveMOowsdl#ReceiveMO', // soapaction
    'rpc', // style
    'encoded', // use
    'Says hello to the caller' // documentation
);

//$server->register('ReceiveMO');

function ReceiveMO($moid, $moseq, $src, $dest, $cmdcode, $msgbody, $opid, $username, $password) {	
	$arr_data = explode(' ', $msgbody);
	
	if (empty($arr_data[1])) {
		$return_code = 400;
		$serial_code = '';
		$status = 'wrong format';
	} else {
		$return_code = 200;
		$serial_code = trim($arr_data[1]);
		$status = 'ok';
	}	
	
	$JConfig = new JConfig;
	
	$today = date("Y-m-d H:i:s");
	
	$conn = mysql_connect($JConfig->host, $JConfig->user, $JConfig->password) or die("false:Connecting database failed");
	mysql_select_db($JConfig->db, $conn) or die("false:Selecting database failed");
	mysql_query("set names 'utf8'", $conn);

	$sql_insert= 'INSERT INTO tbl_sms(phone_customer, sms_date, serial_search, status) ';
	$str_value.= 'VALUES("'.$src.'", "'.$today.'", "'.$serial_code.'", "'.$status.'")';	
	$q = mysql_query($sql_insert.$str_value);
	
	SendMT(mysql_insert_id(), $moid, $moseq, $dest, $src, $cmdcode, $opid, $serial_code);

	return $return_code;
}

$request = isset($HTTP_RAW_POST_DATA) ? $HTTP_RAW_POST_DATA : '';
$server->service($request);

function SendMT($mtseq, $moid, $moseq, $src, $dest, $cmdcode, $opid, $serial_code) {
	$JConfig = new JConfig;
	
	$conn = mysql_connect($JConfig->host, $JConfig->user, $JConfig->password) or die("false:Connecting database failed");
	mysql_select_db($JConfig->db, $conn) or die("false:Selecting database failed");
	mysql_query("set names 'utf8'", $conn);
	
	$encrypted_code = md5($serial_code);
	$sql = "SELECT * FROM tbl_product ";
	$sql.= "WHERE encrypted_code='$encrypted_code' ";
	$sql.= "AND status = '" . ManagerHelper::PRODUCT_STATUS_CAN_USE_TEXT . "'";
	
	$q = mysql_query($sql);
	$r = mysql_fetch_array($q);	
	
	$client = new nusoap_client('http://www.mymobile.com.vn/SMSAPIWS/SMSAgentWS.asmx?wsdl',true);
	$client->soap_defencoding = 'UTF-8';
	$client->decode_utf8 = false;
	$reqtime = gmdate("YmdHis", time()+(7*3600));
	
	if ($r) {
		//update status of product to used after searching succeed
		$query = "UPDATE tbl_product ";
		$query.= "SET status = '" . ManagerHelper::PRODUCT_STATUS_USED_TEXT . "' ";
		$query.= "WHERE id = " . $r['id'];
		$update = mysql_query($query);
		
		// truyen ket qua tim duoc vao mess
		$msgbody = "Tap doan CTVN kinh chao:\n";
		$msgbody.= "Loai: ".$r['fishclass_name']."\n";
		$msgbody.= "Nuoi tai: ".$r['pond_name']."\n";
		$msgbody.= "Ngay nuoi: ".date('d-m-Y', strtotime($r['feed_date']))."\n";
		$msgbody.= "Cam on da su dung san pham chinh hang ca tam viet nam (catam.vn)!";
	} else {
		$msgbody = 'Ma so khong dung hoac da su dung truoc do. Xin goi so 01697771777 de biet them chi tiet';
	}	
	
	$param = array(
		'mtseq' => $mtseq,
		'moid' => $moid,
		'moseq' => $moseq,
		'src' => $src,
		'dest' =>$dest,
		'cmdcode' => $cmdcode,
		'msgbody' => $msgbody,
		'msgtype'=> 'Text',
		'msgtitle' => 'pim welcome',
		'mttotalseg' => '1',
		'mtseqref' => '1',
		'cpid' => '374',
		'reqtime' => $reqtime,
		'procresult' => '1',
		'opid' => $opid,
		'username' => 'catam',
		'password' => '9vAIWtmetl',	
	);
	$result = $client->call('SendMT',$param );
	
	// xu ly error
	$err = $client->getError();
}

die;
?>